package com.ktjy.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ktjy.dto.LoginUser;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Resource
    HttpSession session;

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject,"createTime",
                LocalDateTime.class,LocalDateTime.now());

        LoginUser loginUser=(LoginUser)SecurityContextHolder.getContext().
                getAuthentication().getPrincipal();
        this.strictInsertFill(metaObject,"createBy",
                String.class,loginUser.getUsername());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject,"updateTime",
                LocalDateTime.class,LocalDateTime.now());

        LoginUser loginUser=(LoginUser)SecurityContextHolder.getContext().
                getAuthentication().getPrincipal();
        this.strictUpdateFill(metaObject,"updateBy",
                String.class,loginUser.getUsername());
    }
}
